import {
  queryChargingErrorPage,
  queryRecordPage,
  queryUsePage,
  chargingStatistics,
  queryChargingBar,
} from './service';
import { queryDictList } from '@/services/dict';
import { DICT } from './enum';

export default {
  namespace: 'analyse_charge',

  state: {
    chargingErrorList: { data: [] },
    recordList: { data: [] },
    useList: { data: [] },
    details: [],
    chargingErrorCond: {},
    useCond: {},
    recordCond: {},
    dict: {},
    errorPage: {},
    chargingBar: {},
  },

  effects: {
    *initChargingErrorCond(_, { put }) {
      yield put({ type: 'setChargingErrorCond', payload: { pageSize: 10 } });
      yield put({ type: 'queryChargingErrorPage' })
    },
    *updateChargingErrorCond({ payload }, { put, select }) {
      const cond = yield select(state => state.analyse_charge.chargingErrorCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setChargingErrorCond', payload: params });
      yield put({ type: 'queryChargingErrorPage' })
    },
    *queryChargingErrorPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.analyse_charge.chargingErrorCond);  
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryChargingErrorPage, params);
      yield put({ type: 'setChargingErrorPage', payload: { list, total } })
    },
    *updateRecordCond({ payload }, { put, select }) {
      const cond = yield select(state => state.analyse_charge.recordCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setRecordCond', payload: params });
      yield put({ type: 'queryRecordPage' })
    },
    *queryRecordPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.analyse_charge.recordCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryRecordPage, params);
      yield put({ type: 'setRecordPage', payload: { list, total } })
    },
    *initUseCond(_, { put }) {
      yield put({ type: 'setUseCond', payload: { pageSize: 10 } });
      yield put({ type: 'queryUsePage' })
    },
    *updateUseCond({ payload }, { put, select }) {
      const cond = yield select(state => state.analyse_charge.useCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setUseCond', payload: params });
      yield put({ type: 'queryUsePage' })
    },
    *queryUsePage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.analyse_charge.useCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryUsePage, params);
      yield put({ type: 'setUsePage', payload: { list, total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *chargingStatistics(_, { call, put }) {
      const response = yield call(chargingStatistics);
      yield put({ type: 'setStatistics', payload: response })
    },
    *queryChargingBar({ payload }, { call, put }) {
      const response = yield call(queryChargingBar, payload);
      yield put({ type: 'setChargingBar', payload: response })
    }
  },

  reducers: {
    setChargingErrorCond(state, action) {
      return {
        ...state,
        chargingErrorCond: action.payload
      }
    },
    setChargingErrorPage(state, action) {
      return {
        ...state,
        chargingErrorList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setUseCond(state, action) {
      return {
        ...state,
        useCond: action.payload
      }
    },
    setUsePage(state, action) {
      return {
        ...state,
        useList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setRecordCond(state, action) {
      return {
        ...state,
        recordCond: action.payload
      }
    },
    setRecordPage(state, action) {
      return {
        ...state,
        recordList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setStatistics(state, action) {
      return {
        ...state,
        statistics: action.payload
      }
    },
    setErrorPage(state, action) {
      return {
        ...state,
        errorPage: action.payload
      }
    },
    setChargingBar(state, action) {
      return {
        ...state,
        chargingBar: action.payload
      }
    }
  }
}
